package cn.jolyoulu.adminweb.controller.sys;


import cn.jolyoulu.common.web.security.permission.Permission;
import cn.jolyoulu.adminweb.entity.bo.query.SysUserQueryBO;
import cn.jolyoulu.adminweb.entity.vo.SysUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.jolyoulu.adminweb.entity.bo.form.SysUserFormBO;
import cn.jolyoulu.adminweb.service.ApiSysUserService;
import cn.jolyoulu.common.mybatis.plus.plugin.page.JlPage;
import cn.jolyoulu.common.web.entity.ResultInfo;

import java.util.List;

/**
 * 系统管理/用户管理
 */
@RestController
@RequestMapping("/api/sys/user")
public class ApiSysUserController {

    @Autowired
    private ApiSysUserService apiSysUserService;

    /**
     * 列表查询
     */
    @Permission({"sys:user:basic"})
    @GetMapping("/pageList")
    public ResultInfo<JlPage<SysUserVO>> pageList(SysUserQueryBO bo){
        JlPage<SysUserVO> res = apiSysUserService.pageList(bo);
        return ResultInfo.success(res);
    }

    /**
     * 获取信息
     */
    @Permission({"sys:user:basic"})
    @GetMapping("/get/{id}")
    public ResultInfo<SysUserVO> getById(@PathVariable String id) {
        SysUserVO res = apiSysUserService.getById(id);
        return ResultInfo.success(res);
    }

    /**
     * 新增用户
     */
    @Permission({"sys:user:add"})
    @PostMapping("/add")
    public ResultInfo add(@RequestBody @Validated SysUserFormBO sysUserFormBO){
        apiSysUserService.add(sysUserFormBO);
        return ResultInfo.success();
    }

    /**
     * 编辑用户
     */
    @Permission({"sys:user:edit"})
    @PutMapping("/edit/{id}")
    public ResultInfo edit(@PathVariable String id,@RequestBody SysUserFormBO sysUserFormBO){
        sysUserFormBO.setId(id);
        apiSysUserService.update(sysUserFormBO);
        return ResultInfo.success();
    }

    /**
     * 删除用户
     */
    @Permission({"sys:user:del"})
    @DeleteMapping("/del")
    public ResultInfo del(@RequestBody List<String> ids){
        apiSysUserService.delByIds(ids);
        return ResultInfo.success();
    }

    /**
     * 分配角色
     */
    @Permission({"sys:role:allot"})
    @PutMapping("/allot/role/{id}")
    public ResultInfo allotRole(@PathVariable String id,@RequestBody List<String> roleIds){
        apiSysUserService.allotRole(id,roleIds);
        return ResultInfo.success();
    }
}
